class ListNode {
    val: number
    next: ListNode | null
    constructor(val?: number, next?: ListNode | null) {
        this.val = (val === undefined ? 0 : val)
        this.next = (next === undefined ? null : next)
    }
}

/**
 * 合并两个排序的链表
 * @param l1 
 * @param l2 
 */
const mergeTwoLists = (l1: ListNode | null, l2: ListNode | null): ListNode | null => {
    // 边界判断
    if (l1 === null) return l2;
    if (l2 === null) return l1

    // 分别指向l1和l2
    let l: ListNode | null = l1,
        r: ListNode | null = l2;

    const dummyHead = new ListNode();
    let head = dummyHead;
    // 从头到尾遍历
    while (l !== null && r !== null) {
        if (l.val < r.val) {
            head = head.next = l
            l = l.next;
        } else {
            head = head.next = r;
            r = r.next
        }
    }

    // l1或者l2可能还没完
    head.next = l === null ? r : l

    return dummyHead.next;
};

export { }